package redBook;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main1 {

    public static void main(String[] args) throws Exception{
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String s = buf.readLine();
        int n = Integer.parseInt(s);
        int whiteCount = 0;
        int blackCount = 0;
        int emptyCount = 0;
        List<Integer> whiteList = new ArrayList<>();
        List<Integer> blackList = new ArrayList<>();
        String[] ss;
        for (int i = 0; i < n; i++){
            s = buf.readLine();
            if (s.startsWith("0")){
                ss = s.split(" ");
                whiteList.add(Integer.parseInt(ss[1]));
                blackList.add(Integer.parseInt(ss[2]));
                emptyCount++;
            } else if ("1".equals(s)){
                whiteCount++;
            } else if ("2".equals(s)){
                blackCount++;
            }
        }
        if (emptyCount == 0){
            System.out.println(0);
        }
        int count = whiteCount - blackCount;
        if (emptyCount < Math.abs(count)){
            System.out.println(-1);
        }
        int blackLen = 0;
        int whiteLen = 0;
        if (count > 0){
            blackLen = count;
            if ((emptyCount - count) % 2 == 0 ){
                blackLen += (emptyCount - count)/2;
                whiteLen = (emptyCount - count)/2;
            } else {
                System.out.println(-1);
            }
        } else if (count < 0){
            whiteLen = -count;
            if ((emptyCount + count) % 2 == 0 ){
                blackLen = (emptyCount + count)/2;
                whiteLen += (emptyCount + count)/2;
            } else {
                System.out.println(-1);
            }
        } else {
            if (emptyCount % 2 == 0){
                blackLen = emptyCount/2;
                whiteLen = emptyCount/2;
            } else {
                System.out.println(-1);
            }
        }
        int[][] dp = new int[whiteLen + 1][blackLen + 1];
        for (int i = 1; i <= blackLen; i++){
            dp[0][i] = dp[0][i - 1] + blackList.get(i);
        }
        for (int i = 1; i <= whiteLen; i++){
            dp[i][0] = dp[i - 1][0] + whiteList.get(i);
        }
        for (int i = 1; i <= whiteLen; i++){
            for (int j = 1; j <= blackLen; j++){
                dp[i][j] = Math.min(dp[i][j - 1] + blackList.get(j), dp[i - 1][j] + whiteList.get(i));
            }
        }
        System.out.println(dp[whiteLen][blackLen]);

    }
}
